package lcr;

public class Test0099 {
}

class Solution0099_1 {
    public int minPathSum(int[][] grid) {
        int length1 = grid.length;
        int length2 = grid[0].length;

        for (int i = 0; i < length1; i++)
            for (int j = 0; j < length2; j++)
                if (i-1>=0&&j-1>=0) grid[i][j]+=Math.min(grid[i-1][j],grid[i][j-1]);
                else if (i-1>=0||j-1>=0) grid[i][j]+=i-1>=0?grid[i-1][j]:grid[i][j-1];

        return grid[length1-1][length2-1];
    }
}
